<?php
/**
 * Created by PhpStorm.
 * User: xcx
 * Date: 2017/6/2
 * Time: 10:17
 */

class Controller_Adminmall_Ad extends Controller_Adminmall_Abstract
{
    private $DB_SERVER = "xhs_slave";
    private $DB_SERVER_MASTER = "xhs_master";
    /**
     * 广告图管理列表
     */
    public function listAction(){
        global $_F;
//        $this->openDebug();
        $page = FRequest::getInt("page");
        $table = new FTable('ad', '', $this->DB_SERVER);
        $list = $table->order("id desc")->limit(20)->page($page)->select();
//        FResponse::output($list);exit();
        $pagerInfo = $table->getPagerInfo();
        $this->assign("list", $list);
        $this->assign('page_info', $pagerInfo);
        $img_oss = FConfig::get("global.s_url_oss").'/uploads/';
        $this->assign('img_oss', $img_oss);
//        var_dump($list);
        $this->display("ad/list");
    }

    /*
     * 广告图添加修改
     */
    public function modifyAction(){
        global $_F;
//        $this->openDebug();
        $id = FRequest::getInt("id");
        if($this->isPost()){
            $id = FRequest::getPostInt('id');
            $status = FRequest::getPostInt('status');
            $paixu = FRequest::getPostInt('paixu');
            $j_url = FRequest::getPostString('j_url');

            $save = array();
            //图标
            if (!empty($_FILES["url"]['name'])) {
                $img2_url = Service_Attach::getInstance()->uploadOSS('url','xhs/goods','jpg');
                $save['url'] = $img2_url;
            }

            if($paixu){
                $save['paixu'] = $paixu;
            }

            if($status){
                $save['status'] = $status;
            }

            if($j_url){
                $save['j_url'] = $j_url;
            }

            $table = new FTable("ad", "", 'xhs_master');
            if($id){
                $table->update1($save, array("id" => $id));
                $this->success("修改成功", $_F['uri'] . "?id=" . $id);
            }else{
                $ret = $table->insert($save);
                if($ret){
                    $this->success("添加成功", "/Ad/list");
                }else{
                    $this->error("添加失败", $_F['refer']);
                }
            }

        }else{
            $table =  new FTable("ad", "", $this->DB_SERVER);
            $data = $table->fields("*")->where(array("id" => $id))->find();
            $this->assign("data", $data);
        }
        $this->assign("oss_url", FConfig::get("global.s_url_oss")."/uploads/");
        $this->display("ad/modify");
    }

    /**
     * 禁用广告图
     */
    public function statusAdAction(){
        global $_F;
        $id = FRequest::getInt("id");
        $status = FRequest::getInt('status');
        if($id && $status){
            $table = new FTable("ad", "", $this->DB_SERVER);
            $ret = $table->where(array("id" => $id))->save(array("status" => $status));
            if($ret){
                $this->success("操作成功", $_F['refer']);
            }else{
                $this->error("操作失败", $_F['refer']);
            }
        }else{
            $this->error("参数为空", $_F['refer']);
        }
    }


}